package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> findNursingProjectPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> list = nursingProjectMapper.findNursingProjectPage(name, status);
        list.forEach(nursingProjectVo -> {
            nursingProjectVo.setCount(nursingProjectMapper.findRefCount(nursingProjectVo.getId()));
        });
        return PageResponse.of(list, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo findNursingProjectById(Long id) {
        NursingProject nursingProjectById = nursingProjectMapper.findNursingProjectById(id);
        return BeanUtil.toBean(nursingProjectById, NursingProjectVo.class);
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);
    }

    @Override
    public void updateNursingProjectByStatus(Long id, Integer status) {
        nursingProjectMapper.updateNursingProjectByStatus(id, status);
    }

    @Override
    public List<NursingProjectVo> findAllNursingProject() {
        return nursingProjectMapper.findAllNursingProject();
    }
}
